package com.ggh.jinjilive.ui.mine.ranking.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemRanKingBangBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mine.ranking.RankingBangModel;

import java.util.List;

public class RankingBangAdapter extends RecyclerView.Adapter<RankingBangAdapter.ItemViewHolder> {

    private List<RankingBangModel> rankingBangModelList;

    public void add(List<RankingBangModel> rankingBangModelList) {
        this.rankingBangModelList = rankingBangModelList;
        notifyDataSetChanged();
    }


    public void setAdd(List<RankingBangModel> rankingBangModelList) {
        this.rankingBangModelList = rankingBangModelList;
        notifyDataSetChanged();
    }

    private AddItemOnClickListener addItemOnClickListener;
    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRanKingBangBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_ran_king_bang, parent, false);
        return new ItemViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        RankingBangModel rankingBangModel = rankingBangModelList.get(position);
        if(null != rankingBangModel){
            //图片加载
            Glide.with(holder.binding.storeImg).load(rankingBangModel.getHead_portrait()).into(holder.binding.storeImg);
            if(position == 0){
                holder.binding.imgRanking.setImageResource(R.mipmap.icon_first);
                holder.binding.imgSort.setImageResource(R.mipmap.icon_jp_phb);
            }else if(position == 1){
                holder.binding.imgRanking.setImageResource(R.mipmap.icon_second);
                holder.binding.imgSort.setImageResource(R.mipmap.icon_yp_phb);
            }else if(position == 2){
                holder.binding.imgRanking.setImageResource(R.mipmap.icon_thirdly);
                holder.binding.imgSort.setImageResource(R.mipmap.icon_tp_phb);
            }else {
                holder.binding.imgRanking.setVisibility(View.INVISIBLE);
                holder.binding.imgSort.setVisibility(View.INVISIBLE);
            }
            //名称
            holder.binding.tvDesignation.setText(rankingBangModel.getNickname());
            //钻石
            holder.binding.totalPrice.setText(rankingBangModel.getTotal_price() +"钻石");
            //级别
            holder.binding.tvLevel.setText(rankingBangModel.getLevel() +"");
            //级别图片
            Glide.with(holder.binding.levelImg).load(rankingBangModel.getLevel_img()).into(holder.binding.levelImg);
            //性别
            if(rankingBangModel.getSex() == 1){
                holder.binding.imgSex.setImageResource(R.mipmap.ic_male);
            }else {
                holder.binding.imgSex.setImageResource(R.mipmap.ic_female);
            }
            if(rankingBangModel.getIs_follow() == 1){
                holder.binding.attentionButton.setBackgroundResource(R.drawable.bg_50_0080ff);
                holder.binding.attentionButton.setText("已关注");
            }else {
                holder.binding.attentionButton.setBackgroundResource(R.drawable.bg_1_50_f);
                holder.binding.attentionButton.setText("关注");
            }
            //关注
            holder.binding.attentionButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(null != addItemOnClickListener){
                        addItemOnClickListener.onSkipClicked(position,rankingBangModel);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == rankingBangModelList ? 0 : rankingBangModelList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemRanKingBangBinding binding;

        public ItemViewHolder(@NonNull ItemRanKingBangBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
